<?php
/**
 * CategoryController.php
 *
 * 用户信息分类控制器
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Home
 * @package		Home_Controller
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Controller_Public */
//require_once 'Momoger/Controller/Public.php';

/**
 * 相册 分类 控制器
 *
 * @category	Home
 * @package		Home_Controller
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class CategoryController extends Momoger_Controller_Public {

	/**
	 * 控制器初始化
	 *
	 * @return void
	 */
	public function mmInit() {
		if (!$this->user) {
			//$this->redirectWithMsg(2, 'You have not login, Please login first!', NULL, '/login');
			//return ;
			exit();
		}

		// close layout
		if (null !== ($layout = Zend_Layout::getMvcInstance())) {
            $layout->disableLayout();
        }
	}

	/**
	 * 相册分类默认显示接口
	 *
	 * @return HTML
	 */
	public function indexAction() {
		//get the category type
		$type = $this->getRequest()->getUserParam('type');
		$this->view->categoryType = $type;

		//get use exists categories
		$category = new Momoger_Model_Category();
		$this->view->categories = $category->getCategories($this->user->userID, $type);
	}

	/**
	 * 添加新的 分类信息
	 *
	 * @return JSON
	 */
	public function addAction() {
		//init repnose data
		$jsonRs = array('result' => FALSE, 'msg' => '');

		//get post data
		$filter = new Zend_Filter_StripTags();
		$categoryName = $filter->filter($this->getRequest()->getParam('name'));
		$categoryParent = (int)$this->getRequest()->getParam('parent');

		$type = $this->getRequest()->getParam('type');
		if (!$type || !in_array($type, array('article', 'photo'))) {
			//validate false
			$jsonRs['msg'] = 'The category type is invalid!';

			//reponse data to request client
			$this->getHelper('Json')->sendJson($jsonRs);

			//log it
			//$this->logger->debug($jsonRs['msg']);

			return FALSE;
		}

		//validate data
		$validator = new Zend_Validate_StringLength(2,20);
		if (!$validator->isValid($categoryName)) {
			//validate false
			$jsonRs['msg'] = 'The category name is invalid! Name length allowed 2 - 20 chars.';

			//reponse data to request client
			$this->getHelper('Json')->sendJson($jsonRs);

			//log it
			//$this->logger->debug($jsonRs['msg']);

			return FALSE;
		}

		//get data model
		$category = new Momoger_Model_Category();

		//validate the category is exists or not
		if ($category->categoryIsExists($this->user->userID, $categoryName, $type)) {
			//validate false
			$jsonRs['msg'] = 'The category name is exists!';

			//reponse data to request client
			$this->getHelper('Json')->sendJson($jsonRs);

			//log it
			$this->logger->debug($jsonRs['msg']);

			return FALSE;
		}

		//save category
		$category->set(array('userID' => $this->user->userID, 'categoryName' => $categoryName, 'categoryType' => $type, 'categoryParentID' => $categoryParent))->save();

		//update reponse data
		$jsonRs['result'] = TRUE;
		$jsonRs['msg'] = 'Congratulations! New category saved success!';

		//reponse data to request client
		$this->getHelper('Json')->sendJson($jsonRs);
		return TRUE;
	}

	/**
	 * 删除分类信息
	 *
	 * @return JSON
	 */
	public function deleteAction() {
		//init repnose data
		$jsonRs = array('result' => TRUE, 'msg' => '');

		//reponse data to request client
		$this->getHelper('Json')->sendJson($jsonRs);

		return TRUE;
	}
}